{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "extensions": {
     "jupyter_dashboards": {
      "version": 1,
      "views": {
       "grid_default": {},
       "report_default": {
        "hidden": false
       }
      }
     }
    }
   },
   "source": [
    "# Compare TensorRT Engines\n",
    "\n",
    "Use this Jupyter worksheet to compare two or more TensorRT Engine plans."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "extensions": {
     "jupyter_dashboards": {
      "version": 1,
      "views": {
       "grid_default": {},
       "report_default": {
        "hidden": false
       }
      }
     }
    }
   },
   "source": [
    "## Load JSON Files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "extensions": {
     "jupyter_dashboards": {
      "version": 1,
      "views": {
       "grid_default": {},
       "report_default": {
        "hidden": true
       }
      }
     }
    }
   },
   "outputs": [],
   "source": [
    "import IPython\n",
    "from ipywidgets import widgets\n",
    "from trex import *\n",
    "from trex.notebook import *\n",
    "from trex.report_card import *\n",
    "from trex.compare_engines import *\n",
    "\n",
    "# Configure a wider output (for the wide graphs)\n",
    "set_wide_display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "extensions": {
     "jupyter_dashboards": {
      "version": 1,
      "views": {
       "grid_default": {},
       "report_default": {
        "hidden": true
       }
      }
     }
    }
   },
   "outputs": [],
   "source": [
    "engine_name_1 = \"../tests/inputs/mobilenet.qat.onnx.engine\"\n",
    "engine_name_2 = \"../tests/inputs/mobilenet_v2_residuals.qat.onnx.engine\"\n",
    "\n",
    "def extract_engine_name(engine_path):\n",
    "    from pathlib import Path \n",
    "    return Path(engine_path).name\n",
    "\n",
    "def make_plan(engine_path, engine_name=None):\n",
    "    plan = EnginePlan(f'{engine_path}.graph.json', f'{engine_path}.profile.json', f\"{engine_path}.profile.metadata.json\", name=engine_name)\n",
    "    return plan\n",
    "    \n",
    "plan1 = make_plan(engine_name_1, \"mobilenet.qat\")\n",
    "plan2 = make_plan(engine_name_2, \"mobilenet_v2_residuals.qat\")\n",
    "plans = (plan1, plan2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "extensions": {
     "jupyter_dashboards": {
      "version": 1,
      "views": {
       "grid_default": {},
       "report_default": {
        "hidden": false
       }
      }
     }
    }
   },
   "source": [
    "## Summary\n",
    "\n",
    "It is helpful to look at a high-level summary of the engine plan before diving into the details."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "compare_engines_overview(plans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "compare_engines_summaries_tbl(plans, orientation='vertical')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "compare_engines_layer_latencies(\n",
    "    plan1, plan2,\n",
    "    # Allow for 3% error grace threshold when color highlighting performance differences\n",
    "    threshold=0.03,\n",
    "    # Inexact matching uses only the layer's first input and output to match to other layers.\n",
    "    exact_matching=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "compare_engines_layer_details(plans[0], plans[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(plan1.name)\n",
    "report_card_perf_overview_widget(plan1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(plan2.name)\n",
    "report_card_perf_overview_widget(plan2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(plan1.name)\n",
    "report_card_table_view(plan1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(plan2.name)\n",
    "report_card_table_view(plan2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for plan in plans:\n",
    "    graph = graphing.to_dot(plan, graphing.layer_type_formatter, display_regions=True, expand_layer_details=True)\n",
    "    graphing.render_dot(graph, plan.name, 'svg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "extensions": {
   "jupyter_dashboards": {
    "activeView": "report_default",
    "version": 1,
    "views": {
     "grid_default": {
      "name": "grid",
      "type": "grid"
     },
     "report_default": {
      "name": "report",
      "type": "report"
     }
    }
   }
  },
  "interpreter": {
   "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
